package at.fhj.pse.pageobjects;

import static org.junit.Assert.fail;

import java.util.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class TimeChartPage{
    private WebDriver driver;
    
    /*
     * Constructor
     */
    public TimeChartPage(WebDriver driver){
        this.driver = driver;
    }
    

    /*
     * Input field: username:String
     */
    private String username;
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
    
    
    /*
     * Input field: password:String
     */
    private String password;
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    
	/*
	 * Input field: acceptNextAlert:Boolean
	 */
	private Boolean acceptNextAlert;
	public Boolean getacceptNextAlert(){
		return acceptNextAlert;
	}
	public void setacceptNextAlert(Boolean acceptNextAlert){
		this.acceptNextAlert = acceptNextAlert;
	}
	/*
	 * Input field: From
	 */
	private String from;
	public String getFrom() {
		
		return from;
	}
	public void setFrom(String string) {
		
		this.from = string;
	}
	/*
	 * Input field: to
	 */
	private String to;
	public String getTo() {
		
		return to;
	}
	public void setTo(String string) {
		
		this.to = string;
	}
	/*
	 * Action: TimeChartPage
	 */
	public Boolean showTimeChart()throws InterruptedException{
		driver.findElement(By.id("j_idt35:intervalsFrom_input")).sendKeys(getFrom());
		driver.findElement(By.id("j_idt35:intervalsTo_input")).sendKeys(getTo());
		driver.findElement(By.id("j_idt35:j_idt38")).click();
		
		Boolean smsg = getSelectorMessage("canvas.jqplot-event-canvas");
		return smsg;
	}
	
	/*
	 * Action: SelectorMessage
	 */
	public Boolean getSelectorMessage(String selector) throws InterruptedException{
		Boolean isTrue = isElementPresent(By.cssSelector(selector));
		for (int second = 0;; second++) {
	    	if (second >= 60) fail("timeout");
	    	try { if (isElementPresent(By.cssSelector(selector))) break; } catch (Exception e) {}
	    	Thread.sleep(1000);
	    }
		return isTrue;
	}

	/*
	 * Action: PathMessage
	 */
	public String getPathMessage(String expected, String Path) throws InterruptedException{
		String xPath = driver.findElement(By.xpath(Path)).getText();
		for (int second = 0;; second++) {
			if (second >= 60) fail("timeout");
			try { if (expected.equals(driver.findElement(By.xpath(Path)).getText())) break; } catch (Exception e) {}
			Thread.sleep(1000);
		}
		return xPath;
	}

	/*
	 * Action: MonthLabel
	 */
	public void MonthLabel(int panelid){
		driver.findElement(By.id("j_idt35:month_label")).click();
	    driver.findElement(By.xpath("//div[@id='j_idt35:month_panel']/div/ul/li[" + panelid + "]")).click();
	    driver.findElement(By.id("j_idt35:j_idt41")).click();
	}
	
	/*
	 * Action: ElementPresent
	 */
	 private boolean isElementPresent(By by) {
       try {
         driver.findElement(by);
         return true;
       } catch (NoSuchElementException e) {
         return false;
       }
     }

   /*
	 * Action: AlertPresent
	 */
	/* private boolean isAlertPresent() {
       try {
         driver.switchTo().alert();
         return true;
       } catch (NoAlertPresentException e) {
         return false;
       }
     }

     /*
	 * Action: AlertText
	 */
	/*private String closeAlertAndGetItsText() {
       try {
         Alert alert = driver.switchTo().alert();
         String alertText = alert.getText();
         if (acceptNextAlert) {
           alert.accept();
         } else {
           alert.dismiss();
         }
         return alertText;
       } finally {
         acceptNextAlert = true;
       }
     }*/

}
